package com.yxs.school.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxs.school.api.entity.User;
import com.yxs.school.api.entity.UserVo;
import com.yxs.school.api.mapper.UserMapper;
import com.yxs.school.util.DateUtil;
import com.yxs.school.util.Empty;
import com.yxs.school.util.md5.Md5;
import com.yxs.school.api.service.IUserService;
import com.yxs.school.result.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author whp
 * @since 2019-11-15
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Resource
    private IUserService iUserService;
    @Resource
    private UserMapper userMapper;
    @PostMapping("register")
    public R register(@RequestBody User user) {
        String pwd = user.getPassword();
        user.setPassword(Md5.encode(pwd));
        String phone = user.getPhone();
        User exit= iUserService.getOne(new QueryWrapper<User>().eq("phone",phone));
        if (exit!=null) {
            return R.fail(415,"用户已注册");
        }
        if ("".equals(user.getPassword()))
            return R.fail(400,"操作失败");
        if (Empty.isEmpty(user.getUserName()))
            user.setUserName(phone);
        boolean flag = iUserService.save(user);
        if (flag) {
            return R.success("成功");
        }else {
            return R.fail(400,"操作失败");
        }
    }
    @PostMapping("login")
    public R login(@Param("userName") String userName,@Param("password") String password, HttpSession session) {
        String pwd = Md5.encode(password);
        User user1= iUserService.getOne(new QueryWrapper<User>().eq("phone",userName));
        if(user1 == null) {
            return R.fail(400,"用户不存在");
        }
        if (pwd.equals(user1.getPassword())) {
            session.setAttribute("user",user1);
            user1.setLastLogin(DateUtil.getDate());
            user1.setPassword(null);
            boolean flag= iUserService.updateById(user1);
            if (flag)
            return R.data(200,user1,"登录成功！");
            else
                return R.fail(400,"密码错误");
        }else {
            return R.fail(400,"密码错误");
        }
    }
    @PostMapping("update")
    public R updatePassword(@RequestBody User user) {
        String pwd = user.getPassword();
        //User user1=iUserService.getById(user.getId());
        if (Empty.isEmpty(pwd))
            user.setPassword(null);
        else
        user.setPassword(Md5.encode(pwd));
        boolean flag = iUserService.updateById(user);
        if (flag) {
            return R.data(200,null,"成功");
        }else {
            return R.fail(400,"操作失败");
        }
    }
    @PostMapping("delete")
    public R delete(Integer id) {
        boolean flag = iUserService.removeById(id);
        if (flag)
            return R.data(200,null,"成功");
        else {
            return R.fail(400,"操作失败");
        }
    }
    @GetMapping("active")
    public R active() {
        List<UserVo> users=userMapper.activeUser(
                new QueryWrapper<User>().orderByDesc("last_login")
//                .select("id", "user_name",  "rel_name", "phone", "email", "sex", "birthday", "reg_time", "last_login")
        );
        if (users.isEmpty())
            return R.fail(400,"查询失败");
        else
            return R.data(200,users,"");
    }
}
